#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2021/7/2 16:41
# @Author    :cjw
import allure
from appium.webdriver.common.mobileby import MobileBy
from selenium.common.exceptions import TimeoutException

from app_wework_po.util.log import logger
from app_wework_po.setting import ELEMENT_YML
from app_wework_po.base.base_page import BasePage
from app_wework_po.util.file_operator import YmlOperator
from app_wework_po.util.handle_uiselector import handle_text, handle_result_tuple


class EditMemberPage(BasePage):
	"""手动输入添加成员页面"""

	# 添加元素定位数据到locators
	BasePage.locators.update(YmlOperator(ELEMENT_YML['emp']).data)

	def edit_member_quick_input(self, name: str, phone: str):
		"""
		编辑添加成员，快速输入
		:param name: 姓名
		:param phone: 手机
		:return:
		"""
		from app_wework_po.page.add_member_page import AddMemberPage

		self._check_quick_input()
		with allure.step(msg := f'输入姓名：{name}'):
			logger.info(msg)
			self.send_keys(self.name, name)
		with allure.step(msg := f'输入手机号: {phone}'):
			logger.info(msg)
			self.send_keys(self.phone, phone)
		with allure.step(msg := '取消勾选“保存后自动发送邀请通知”'):
			logger.info(msg)
			self.tap(self.send_notice)
		with allure.step(msg := '点击“保存”'):
			logger.info(msg)
			self.tap(self.save)
		return AddMemberPage(self.driver)

	def edit_member_full_input(self, name: str, account: str, phone: str, email: str):
		"""
		编辑添加成员，完整输入
		:param name: 姓名
		:param account: 帐号
		:param phone: 手机
		:param email: 邮箱
		:return:
		"""
		from app_wework_po.page.add_member_page import AddMemberPage

		self._check_full_input()
		with allure.step(msg := f'输入姓名：{name}'):
			logger.info(msg)
			self.send_keys(self.name_full_input, name)
		with allure.step(msg := f'输入帐号：{account}'):
			logger.info(msg)
			self.send_keys(self.account, account)
		with allure.step(msg := f'输入手机号: {phone}'):
			logger.info(msg)
			self.send_keys(self.phone_must, phone)
		with allure.step(msg := f'输入邮箱: {email}'):
			logger.info(msg)
			self.send_keys(self.email, email)
		with allure.step(msg := '取消勾选“保存后自动发送邀请通知”'):
			logger.info(msg)
			self.swipe_find_click(handle_result_tuple(self.locators['send_notice'][1]))
		with allure.step(msg := '点击“保存”'):
			logger.info(msg)
			self.tap(self.save)
		return AddMemberPage(self.driver)

	def _check_quick_input(self):
		"""检查快速输入"""
		try:
			with allure.step(msg := '点击“快速输入, 进入快速输入页面”'):
				logger.info(msg)
				self.find_click(handle_result_tuple(self.locators['quick_input'][1]), timeout=2)
		except TimeoutException:
			logger.info('未找到“快速输入”')

	def _check_full_input(self):
		"""检查完整输入"""
		try:
			with allure.step(msg := '点击“完整输入, 进入完整输入页面”'):
				logger.info(msg)
				self.find_click(handle_result_tuple(self.locators['full_input'][1]), timeout=2)
		except TimeoutException:
			logger.info('未找到“完整输入”')
